package com.hjm.project.profitDay.detailed.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.terminal.manufacturer.service.IManufacturerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.profitDay.detailed.domain.ProfitDayDetailed;
import com.hjm.project.profitDay.detailed.service.IProfitDayDetailedService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 分润日结明细Controller
 * 
 * @author maker-platform
 * @date 2021-01-16
 */
@Controller
@RequestMapping("/profitDay/detailed")
public class ProfitDayDetailedController extends BaseController
{
    private String prefix = "profitDay/detailed";

    @Autowired
    private IProfitDayDetailedService profitDayDetailedService;
    @Resource
    private IManufacturerService manufacturerService;


    @RequiresPermissions("profitDay:detailed:view")
    @GetMapping()
    public String detailed(ModelMap modelMap,ProfitDayDetailed profitDayDetailed) throws Exception {
        //获取分润汇总信息
        if(false){  //默认不查询
            List<Map<String,Object>> profitDayDetaileds = profitDayDetailedService.selectProfitDayDetailedListSum(profitDayDetailed);
            for (Map<String, Object> dayDetailed : profitDayDetaileds) {
                String ptype=String.valueOf(dayDetailed.get("PTYPE"));
                if ("0".equals(ptype)){
                    modelMap.put("sumOneList",dayDetailed);
                }else if("1".equals(ptype)){
                    modelMap.put("sumGroupList",dayDetailed);
                }else if("2".equals(ptype)){
                    modelMap.put("trade",dayDetailed);
                }
            }
        }
        List<Map> manufacturer = manufacturerService.getManufacturer(ShiroUtils.isAgent() ? ShiroUtils.getLoginAgentNum() : null);
        modelMap.put("manufacturer",manufacturer);

        return prefix + "/detailed";
    }
    @PostMapping("/totalRefresh")
    @ResponseBody
    public AjaxResult totalRefresh(ProfitDayDetailed profitDayDetailed) throws Exception
    {

        profitDayDetailed.setLoginAgentNum(ShiroUtils.getLoginAgentNum());


        //获取分润汇总信息
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> profitDayDetaileds = profitDayDetailedService.selectProfitDayDetailedListSum(profitDayDetailed);
        for (Map<String, Object> dayDetailed : profitDayDetaileds) {
            String ptype=String.valueOf(dayDetailed.get("PTYPE"));
            if ("0".equals(ptype)){
                map.put("oneProfitBerforTax",dayDetailed.get("PROFIT_BERFORTAX"));
                map.put("oneTaxMoney",dayDetailed.get("TAX_MONEY"));
                map.put("oneProfit",dayDetailed.get("PROFIT"));
            }else if("1".equals(ptype)){
                map.put("groupProfitBerforTax",dayDetailed.get("PROFIT_BERFORTAX"));
                map.put("groupTaxMoney",dayDetailed.get("TAX_MONEY"));
                map.put("groupProfit",dayDetailed.get("PROFIT"));
            }else if("2".equals(ptype)){
                map.put("sunCount",dayDetailed.get("SUM_COUNT"));
                map.put("amount",dayDetailed.get("AMOUNT"));
                map.put("tradeCharges",dayDetailed.get("TRADE_CHARGES"));
            }
        }
        return AjaxResult.success(map);
    }


    /**
     * 查询分润日结明细列表
     */
    @RequiresPermissions("profitDay:detailed:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProfitDayDetailed profitDayDetailed)
    {
        if (ShiroUtils.isAgent()) {
            profitDayDetailed.setLoginAgentNum(ShiroUtils.getLoginAgentNum());
        }
        startPage();
        List<ProfitDayDetailed> list = profitDayDetailedService.selectProfitDayDetailedList(profitDayDetailed);
        return getDataTable(list);
    }


    /**
     * 导出分润日结明细列表
     */
    @RequiresPermissions("profitDay:detailed:export")
    @Log(title = "分润日结明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProfitDayDetailed profitDayDetailed)
    {
        List<ProfitDayDetailed> list = profitDayDetailedService.selectProfitDayDetailedList(profitDayDetailed);
        ExcelUtil<ProfitDayDetailed> util = new ExcelUtil<ProfitDayDetailed>(ProfitDayDetailed.class);
        return util.exportExcel(list, "detailed");
    }

    /**
     * 新增分润日结明细
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存分润日结明细
     */
    @RequiresPermissions("profitDay:detailed:add")
    @Log(title = "分润日结明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProfitDayDetailed profitDayDetailed)
    {
        return toAjax(profitDayDetailedService.insertProfitDayDetailed(profitDayDetailed));
    }

    /**
     * 修改分润日结明细
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ProfitDayDetailed profitDayDetailed = profitDayDetailedService.selectProfitDayDetailedById(id);
        mmap.put("profitDayDetailed", profitDayDetailed);
        return prefix + "/edit";
    }

    /**
     * 修改保存分润日结明细
     */
    @RequiresPermissions("profitDay:detailed:edit")
    @Log(title = "分润日结明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProfitDayDetailed profitDayDetailed)
    {
        return toAjax(profitDayDetailedService.updateProfitDayDetailed(profitDayDetailed));
    }

    /**
     * 删除分润日结明细
     */
    @RequiresPermissions("profitDay:detailed:remove")
    @Log(title = "分润日结明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(profitDayDetailedService.deleteProfitDayDetailedByIds(ids));
    }
}
